package com.mondo.financialwallet.presentation.utils;

import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.StringRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.mondo.financialwallet.presentation.App;
import com.mondo.financialwallet.R;

public class Toasts {

    private Toasts() {

    }

    public static void failure(String message) {
        makeText(
                message,
                App.getInstance().getResources().getColor(R.color.colorFailure),
                R.drawable.ic_failure
        );
    }

    public static void warning(String message){
        makeText(
                message,
                App.getInstance().getResources().getColor(R.color.colorWarning),
                R.drawable.ic_warning
        );
    }

    public static void success(@StringRes int stringRes) {
        makeText(
                stringRes,
                App.getInstance().getResources().getColor(R.color.colorSuccess),
                R.drawable.ic_success
        );
    }

    public static void failure(@StringRes int stringRes) {
        makeText(
                stringRes,
                App.getInstance().getResources().getColor(R.color.colorFailure),
                R.drawable.ic_failure
        );
    }

    public static void warning(@StringRes int stringRes){
        makeText(
                stringRes,
                App.getInstance().getResources().getColor(R.color.colorWarning),
                R.drawable.ic_warning
        );
    }

    private static void makeText(@StringRes int stringRes, @ColorInt int color, @DrawableRes int drawableRes) {
        App context = App.getInstance();
        makeText(context.getResources().getString(stringRes), color, drawableRes);
    }

    private static void makeText(String stringRes, @ColorInt int color, @DrawableRes int drawableRes) {
        App context = App.getInstance();
        Toast toast = new Toast(context);
        View root = LayoutInflater.from(context).inflate(R.layout.toast_view, null);
        ((ImageView)root.findViewById(R.id.status_image_view)).setImageResource(drawableRes);
        TextView messageTextView = root.findViewById(R.id.message_text_view);
        messageTextView.setTextColor(color);
        messageTextView.setText(stringRes);
        toast.setView(root);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
    }
}
